package com.timothy.android.answer;

import java.util.ArrayList;
import java.util.List;

import com.timothy.android.BaseAnswerActivity;
import com.timothy.android.util.NumberUtil;
import com.timothy.android.util.ValueCopyUtil;
import com.timothy.chart.BaseChart;
import com.timothy.chart.CanvasSetting;
import com.timothy.chart.Cylinder;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CylinderAnswerActivity extends BaseAnswerActivity{
	private final static String LOG_TAG = "CylinderAnswerActivity";
	List<Cylinder> cyliList = new ArrayList<Cylinder>();
	SurfaceView view = null;
	
	public void setView(int orientation){
		switch(orientation){
			case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:view = new VerticalCylinderView(this);break;
			case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:view = new HorizontalCylinderView(this);break;
			default:view = new VerticalCylinderView(this);
		}
	}
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Log.i(LOG_TAG,"onCreate");
//		this.setTitle("CylinderAnswer");
		this.appendTitle("-CylinderAnswer");
		
		Bundle bundle = this.getIntent().getExtras();
		String displayStyle = bundle.getString("displayStyle");
		
		//get answer data list
		for(int i=0;i<chartList.size();i++){
			Log.i(LOG_TAG,"Loop chartList for PieList");
			
			BaseChart baseChart = chartList.get(i);
			Log.i(LOG_TAG,baseChart.toString());
			
			Cylinder cylinder = new Cylinder();
				
			try {
				ValueCopyUtil.valueCopy(baseChart, cylinder);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Log.i(LOG_TAG,cylinder.toString());

			cyliList.add(cylinder);
		}
		
		if("5".equals(displayStyle)){
			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			setView(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);			
		}else if("6".equals(displayStyle)){
			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			setView(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);			
		}
		
		setContentView(view);
		
	}
	

	class VerticalCylinderView extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder mHolder;
		private Canvas canvas;
		private final static float BAR_RATIO = 0.8f;

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			recoverBrightness();
			return super.onTouchEvent(event);
		}
		
		public VerticalCylinderView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);// 设置Surface生命周期回调
		}

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();// 获得画布
			
			int canvasHeight = canvas.getHeight();
			int canvasWidth = canvas.getWidth();
			Log.i(LOG_TAG, "VerticalCylinderView:Canvas Height:"+canvasHeight);
			Log.i(LOG_TAG, "VerticalCylinderView:Canvas Width:"+canvasWidth);
			
			CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,1);
			
			int leftM = setting.getLeftMargin();
			int rightM = setting.getRightMargin();
			int topM = setting.getTopMargin();
			int bottomM = setting.getBottomMargin();
			
			Log.i(LOG_TAG, "VerticalCylinderView:leftM:"+leftM);
			Log.i(LOG_TAG, "VerticalCylinderView:rightM:"+rightM);
			Log.i(LOG_TAG, "VerticalCylinderView:topM:"+topM);
			Log.i(LOG_TAG, "VerticalCylinderView:bottomM:"+bottomM);
			
			canvas.drawColor(Color.BLACK);// 设置画布背景为黑色
			
			//write question
			Paint mPaint = new Paint();
//			mPaint.setColor(Color.parseColor(questionTextColor));		
//			mPaint.setTextSize(textSize);
//			int queX = setting.getLeftMargin();
//			int queY = setting.getTopMargin()/2;
//			Log.i(LOG_TAG, "VerticalCylinderView:queX:"+queX);
//			Log.i(LOG_TAG, "VerticalCylinderView:queY:"+queY);
//			canvas.drawText(answer.getQuestion().getQuestionName(),queX,queY,mPaint); 
//			canvas.save();
			
			mPaint.setColor(Color.parseColor(questionTextColor));		
			mPaint.setTextSize(textSize);
			int queX = setting.getLeftMargin();
			int queY = setting.getTopMargin();
			Log.i(LOG_TAG, "VerticalCylinderView:queX:"+queX);
			Log.i(LOG_TAG, "VerticalCylinderView:queY:"+queY);
			String questionString = baseBean.getQuestionText();
			if("42".equals(type)&&priority!=null){
				questionString = questionString + "(priority = "+priority+")";
			}
			Log.i(LOG_TAG, "VerticalCylinderView:questionString:"+questionString);
			int queLength = questionString.length();
			Log.i(LOG_TAG, "VerticalCylinderView:queLength:"+queLength);
			int line = 0;
			int lineHeight = 0;
			if(queLength>QUESTION_WRAP_LENGTH_VERTIAL){
				Log.i(LOG_TAG, "VerticalCylinderView:draw question text->queLength>"+QUESTION_WRAP_LENGTH_VERTIAL);
				for(line = 0;line<queLength/QUESTION_WRAP_LENGTH_VERTIAL;line++){
					canvas.drawText(questionString.substring(QUESTION_WRAP_LENGTH_VERTIAL*line,QUESTION_WRAP_LENGTH_VERTIAL*(line+1)),queX,queY/4+lineHeight,mPaint);
					Log.i(LOG_TAG, "VerticalCylinderView:start:"+QUESTION_WRAP_LENGTH_VERTIAL*line+";end:"+QUESTION_WRAP_LENGTH_VERTIAL*(line+1));
					lineHeight+= queY/4;
				}
				Log.i(LOG_TAG, "VerticalCylinderView:start:"+QUESTION_WRAP_LENGTH_VERTIAL*line);
				canvas.drawText(questionString.substring(line*QUESTION_WRAP_LENGTH_VERTIAL),queX,queY/4+lineHeight,mPaint);
			}else{
				Log.i(LOG_TAG, "VerticalCylinderView:draw question text->queLength<="+QUESTION_WRAP_LENGTH_VERTIAL);
				canvas.drawText(questionString,queX,queY/2,mPaint);
			}
			canvas.save();
			
			//根据图形个数既图形间距计算出图形宽度
			Log.i(LOG_TAG, "VerticalCylinderView:spaceInCharts:"+setting.getSpace());
			int canvasNoMarginWidth = canvasWidth - setting.getLeftMargin() - setting.getRightMargin();
			double cylinderWidthInclSpace = (canvasNoMarginWidth * 1.0)/cyliList.size();
			int cylinderWidth = (int)(cylinderWidthInclSpace*0.9);
			setting.setSpace((int)(cylinderWidthInclSpace-cylinderWidth));
			Log.i(LOG_TAG, "VerticalCylinderView:barWidth:"+cylinderWidth);
			
			int canvasNoMarginHeight = canvasHeight-setting.getTopMargin()-setting.getBottomMargin();
			
			for(int i=0;i<cyliList.size();i++){
				Cylinder cylinder = cyliList.get(i);
				
				int votes = cylinder.getVotes();
				double votesShare = cylinder.getShare();
				int cylinderHeight = (int) (votesShare * canvasNoMarginHeight * BAR_RATIO);
				
				Log.i(LOG_TAG, "VerticalCylinderView:cylinderHeight:"+cylinderHeight);
				Log.i(LOG_TAG, "VerticalCylinderView:votes:"+votes);
				Log.i(LOG_TAG, "VerticalCylinderView:votesShare:"+votesShare);
				
				//produce answer content
				
				int tlX = (int) (setting.getLeftMargin()+ i * (cylinderWidth + setting.getSpace()));
				int tlY =  canvasHeight - setting.getBottomMargin() - cylinderHeight;
				int brX = tlX + cylinderWidth;
				int brY = canvasHeight - setting.getBottomMargin();
				
				int emboss = cylinderWidth/4;
				
				//draw answer
				mPaint.setTextSize(cylinder.getAnswerTextSize());
				mPaint.setColor(Color.parseColor(cylinder.getAnswerTextColor()));
//				canvas.drawText(cylinder.getAnswerText(), tlX, tlY-emboss-5, mPaint);
				canvas.drawText(String.valueOf(cylinder.getVotes()), tlX, tlY-20, mPaint);
				String formatShare = "0%";
				if(cylinder.getShare()*100>0){
					formatShare = NumberUtil.formatData1Deci(cylinder.getShare()*100)+"%";
				}
				canvas.drawText(formatShare, tlX, tlY-emboss, mPaint);
				
				//draw cylinder
				mPaint.setColor(Color.parseColor(cylinder.getFullColor()));//圆柱顶部颜色
				canvas.drawOval(new RectF(tlX,tlY-emboss,brX,tlY+emboss), mPaint);
				mPaint.setColor(Color.parseColor(cylinder.getFullColor()));//圆柱中间颜色
				canvas.drawRect(tlX,tlY,brX,brY, mPaint);
				mPaint.setColor(Color.parseColor(cylinder.getShadowColor()));//圆柱底部颜色
				canvas.drawOval(new RectF(tlX,brY-emboss,brX,brY+emboss), mPaint);
				canvas.save();
				
				//draw choice
				mPaint.setTextSize(cylinder.getChoiceTextSize());
				mPaint.setColor(Color.parseColor(cylinder.getChoiceTextColor()));
				canvas.drawText(cylinder.getChoiceText(), tlX+cylinderWidth/2,brY+setting.getBottomMargin()/2, mPaint);
				canvas.save();
			}

			//draw border
			mPaint.setColor(Color.WHITE);
			for(int x=1;x<10;x++){
				//draw horizontal lines
				canvas.drawLine(leftM,x* canvasHeight/10, canvasWidth-rightM,x*canvasHeight/10, mPaint);
			}
			for(int y=1;y<10;y++){
				//draw vertical lines
				canvas.drawLine(y* canvasWidth/10,topM, y* canvasWidth/10, canvasHeight-bottomM, mPaint);
			}
			
			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);// 完成绘图后解锁递交画布视图
		}
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
		}
	}
	
	class HorizontalCylinderView extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder mHolder;
		private Canvas canvas;
		private final static float BAR_RATIO = 0.8f;
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			recoverBrightness();
			return super.onTouchEvent(event);
		}
		
		public HorizontalCylinderView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);// 设置Surface生命周期回调
		}

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();// 获得画布
			
			int canvasHeight = canvas.getHeight();
			int canvasWidth = canvas.getWidth();
			Log.i(LOG_TAG, "HorizontalCylinderView:Canvas Height:"+canvasHeight);
			Log.i(LOG_TAG, "HorizontalCylinderView:Canvas Width:"+canvasWidth);
			
			CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,2);
			
			int leftM = setting.getLeftMargin();
			int rightM = setting.getRightMargin();
			int topM = setting.getTopMargin();
			int bottomM = setting.getBottomMargin();
			
			Log.i(LOG_TAG, "VerticalCylinderView:leftM:"+leftM);
			Log.i(LOG_TAG, "VerticalCylinderView:rightM:"+rightM);
			Log.i(LOG_TAG, "VerticalCylinderView:topM:"+topM);
			Log.i(LOG_TAG, "VerticalCylinderView:bottomM:"+bottomM);
			
			canvas.drawColor(Color.BLACK);// 设置画布背景为黑色

//			//write question
//			Paint mPaint = new Paint();
//			mPaint.setColor(Color.parseColor(questionTextColor));		
//			mPaint.setTextSize(textSize);
//			int queX = setting.getLeftMargin();
//			int queY = setting.getTopMargin()/2;
//			Log.i(LOG_TAG, "VerticalCylinderView:queX:"+queX);
//			Log.i(LOG_TAG, "VerticalCylinderView:queY:"+queY);
//			canvas.drawText(answer.getQuestion().getQuestionName(),queX,queY,mPaint); 
//			canvas.save();
			
			//write question
			Paint mPaint = new Paint();
			mPaint.setColor(Color.parseColor(questionTextColor));		
			mPaint.setTextSize(textSize);
			int queX = setting.getLeftMargin();
			int queY = setting.getTopMargin();
			Log.i(LOG_TAG, "VerticalCylinderView:queX:"+queX);
			Log.i(LOG_TAG, "VerticalCylinderView:queY:"+queY);
			
			String questionString = baseBean.getQuestionText();
			if("42".equals(type)&&priority!=null){
				questionString = questionString + "(priority = "+priority+")";
			}
			int queLength = questionString.length();
			Log.i(LOG_TAG, "VerticalCylinderView:queLength:"+queLength);
			int line = 0;
			int lineHeight = 0;
			if(queLength>QUESTION_WRAP_LENGTH_HORIZONTAL){
				Log.i(LOG_TAG, "VerticalCylinderView:draw question text->queLength>"+QUESTION_WRAP_LENGTH_HORIZONTAL);
				for(line = 0;line<queLength/QUESTION_WRAP_LENGTH_HORIZONTAL;line++){
					canvas.drawText(questionString.substring(QUESTION_WRAP_LENGTH_HORIZONTAL*line,QUESTION_WRAP_LENGTH_HORIZONTAL*(line+1)),queX,queY/2+lineHeight,mPaint);
					Log.i(LOG_TAG, "VerticalCylinderView:start:"+QUESTION_WRAP_LENGTH_HORIZONTAL*line+";end:"+QUESTION_WRAP_LENGTH_HORIZONTAL*(line+1));
					lineHeight+= queY/2;
				}
				Log.i(LOG_TAG, "VerticalCylinderView:start:"+QUESTION_WRAP_LENGTH_HORIZONTAL*line);
				canvas.drawText(questionString.substring(line*QUESTION_WRAP_LENGTH_HORIZONTAL),queX,queY/2+lineHeight,mPaint);
			}else{
				Log.i(LOG_TAG, "VerticalCylinderView:draw question text->queLength<="+QUESTION_WRAP_LENGTH_HORIZONTAL);
				canvas.drawText(questionString,queX,queY/2,mPaint);
			}
			canvas.save();
			
			//根据图形个数既图形间距计算出图形宽度
			Log.i(LOG_TAG, "HorizontalCylinderView:spaceInCharts:"+setting.getSpace());
			
			int canvasNoMarginHeight = canvasHeight - setting.getLeftMargin() - setting.getRightMargin();
			double cylinderHeightInclSpace = (canvasNoMarginHeight * 1.0)/cyliList.size();
			int cylinderHeight = (int)(cylinderHeightInclSpace*0.9);
			int cylinderSpace = (int)(cylinderHeightInclSpace*0.1);
			setting.setSpace(cylinderSpace);
			
			Log.i(LOG_TAG, "HorizontalCylinderView:cylinderHeight:"+cylinderHeight);
			Log.i(LOG_TAG, "HorizontalCylinderView:cylinderSpace:"+cylinderSpace);
			
			int canvasNoMarginWidth = canvasWidth-setting.getLeftMargin()-setting.getRightMargin();
			
			for(int i=0;i<cyliList.size();i++){
				Cylinder cylinder = cyliList.get(i);
				
				int votes = cylinder.getVotes();
				double votesShare = cylinder.getShare();
//				int cylinderHeight = (int) (votesShare * canvasHeight);
				int cylinderWidth = (int) (votesShare * canvasNoMarginWidth * BAR_RATIO );
				
				Log.i(LOG_TAG, "HorizontalCylinderView:cylinderHeight:"+cylinderHeight);
				Log.i(LOG_TAG, "HorizontalCylinderView:votes:"+votes);
				Log.i(LOG_TAG, "HorizontalCylinderView:votesShare:"+votesShare);
				
				//produce answer content
				
				int tlX = setting.getLeftMargin();
				int tlY =  setting.getTopMargin()+ (int)(i*(cylinderHeight + setting.getSpace()));
				int brX = tlX + cylinderWidth;
				int brY = tlY + cylinderHeight;
				
				int emboss = cylinderHeight/4;
				
				//draw answer
				mPaint.setTextSize(cylinder.getAnswerTextSize());
				mPaint.setColor(Color.parseColor(cylinder.getAnswerTextColor()));
				canvas.drawText(cylinder.getAnswerText(), brX+emboss, tlY+cylinderHeight/2, mPaint);
				
				//draw cylinder
				mPaint.setColor(Color.parseColor(cylinder.getFullColor()));//圆柱顶部颜色
				canvas.drawOval(new RectF(tlX-emboss,tlY,tlX+emboss,brY), mPaint);
				
				mPaint.setColor(Color.parseColor(cylinder.getFullColor()));//圆柱中间颜色
				canvas.drawRect(tlX,tlY,brX,brY, mPaint);
				
				mPaint.setColor(Color.parseColor(cylinder.getShadowColor()));//圆柱底部颜色
				canvas.drawOval(new RectF(brX-emboss,tlY,brX+emboss,brY), mPaint);
				
				//draw choice
				mPaint.setTextSize(cylinder.getChoiceTextSize());
				mPaint.setColor(Color.parseColor(cylinder.getChoiceTextColor()));
				canvas.drawText(cylinder.getChoiceText(),10,tlY+cylinderHeight/2, mPaint);
				canvas.save();
				
				//draw border
				mPaint.setColor(Color.WHITE);
				for(int x=1;x<10;x++){
					//draw horizontal lines
					canvas.drawLine(leftM,x* canvasHeight/10, canvasWidth-rightM,x*canvasHeight/10, mPaint);
				}
				for(int y=1;y<10;y++){
					//draw vertical lines
					canvas.drawLine(y* canvasWidth/10,topM, y* canvasWidth/10, canvasHeight-bottomM, mPaint);
				}
				
			}

			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);// 完成绘图后解锁递交画布视图
		}
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
		}
	}

    
}
